#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#include"StringPool.h"
#include"variable.h"
#include"Util.h"
#include"ObjectReader.h"
#include<iostream>
#define _FILE_OFFSET_BITS 64
#define __USE_FILE_OFFSET64
#define __USE_LARGEFILE64
#define _LARGEFILE64_SOURCE 
using namespace std;

ObjectReader::ObjectReader(const char *file){
	in = fopen(file, "rb");
}

ObjectReader::~ObjectReader(){
	fclose(in);
}

long ObjectReader::objTell(){
	return ftell(in);
}

int ObjectReader::objSeek(long _Offset){
	return fseek(in, _Offset, SEEK_SET);
}

bool ObjectReader::readBool(){
	bool b;
	fread(&b, sizeof(bool), 1, in);
	return b;
}

int ObjectReader::readInt(){
	int n;
	fread(&n, sizeof(int), 1, in);
	return n;
}

void ObjectReader::readVector(std::vector<int> &v){
	int *vv = NULL;
	int len = readArray(vv);
	for(int i = 0; i < len; i++){
		v.push_back(vv[i]);
	}
	delete[](vv);
}

void ObjectReader::readVector(std::vector<string*> &v){
	char *cstr = new char[10000];
	int size;
	fread(&size, sizeof(int), 1, in);
	for(int i = 0; i < size; i++){
		int length;
		fread(&length, sizeof(int), 1, in);
		fread(cstr, sizeof(char), length +1, in);
		v.push_back(new string(cstr));
	}
	delete[](cstr);
}

void ObjectReader::readVector(std::vector<vector<string*>*> &v){
	int num = readInt();
	for(int i = 0; i < num; i++){
		vector<string*>* vv = new vector<string*>();
		readVector(*vv);
		v.push_back(vv);
	}
}

void ObjectReader::readMap(HashMap &Map){
	int size = readInt();
	for(int i = 0; i < size; i++){
		string* key = readString();
		int value = readInt();
		Map[key] = value;
	}
}

string* ObjectReader::readString(){
	int length;
	fread(&length, sizeof(int), 1, in);
	char *cstr = new char[length + 1];
	fread(cstr, sizeof(char), length +1, in);
	string tmp = string(cstr);
	string *result = pool->get(tmp);
	delete[](cstr);
	return result;
}

